/***
This do-file plots two main paper figures: a binscatter of job postings for
low-education workers vs median rent by county and of job postings for high-education
workers vs median rent by county.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"
local category "Jobs"

* Create required subfolders
cap mkdir "${root}/data/derived/Burning Glass"
cap mkdir "${root}/results/Jobs"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

* Erase output numbers
cap erase "${root}/results/paper numbers/`category'/Job Postings for Low-Education Workers vs Rent (county).yaml"
cap erase "${root}/results/paper numbers/`category'/Job Postings for High-Education Workers vs Rent (county).yaml"
cap erase "${root}/results/paper numbers/`category'/Change in Job Postings for Low-Education Workers vs Median Rent (county).yaml"

*-------------------------------------------------------------------------------
* Load and process data
*-------------------------------------------------------------------------------

project, uses("${root}/data/web_${jobs_2020_1}/data/Job Postings - County - Weekly.csv")
import delimited "${root}/data/web_${jobs_2020_1}/data/Job Postings - County - Weekly.csv", clear
ds bg_posts*

gen date = mdy(month, day_endofweek, year)
rename *grp12 *grp1
rename *grp345 *grp2

keep date *jzgrp* countyfips

greshape long bg_posts_jzgrp, i(date countyfips) j(jobzone)
rename bg_posts_jzgrp bg_posts

* Confirm structure
gisid jobzone date countyfips
drop if mi(bg_posts)
assert !mi(bg_posts)

gen change_april = bg_posts if year(date) == 2020 & month(date) == 4

gcollapse (mean) change_april, by(countyfips jobzone)

* Re-format change variable
replace change_april = change_april * 100

* Merge county-level covariates
rename countyfips county_fips
project, uses("${root}/data/derived/ACS 2014-2018 5-Year County/ACS 2014-2018 County.dta")
merge m:1 county_fips using "${root}/data/derived/ACS 2014-2018 5-Year County/ACS 2014-2018 County.dta", keep(1 3) nogen keepusing(med_2br_2014_2018_est pop_2014_2018_est medhhinc_2014_2018_est)
rename county_fips countyfips

rename (pop_2014_2018_est med_2br_2014_2018_est medhhinc_2014_2018_est) ///
	   (pop_2018 med_2br_2018 medhhinc_2018)

*-------------------------------------------------------------------------------
* Job postings for low-education workers vs median rent
*-------------------------------------------------------------------------------

reg change_april med_2br_2018  if jobzone == 1 [w = pop_2018], r
	local slope : di %4.2f _b[med_2br_2018] * 1000
	local se : di %4.2f _se[med_2br_2018] * 1000

binscatter change_april med_2br_2018 if jobzone == 1 [w = pop_2018] ///
	, ///
	aspectratio(0.5) ///
	xtitle("Median Two Bedroom Monthly Rent in 2014-2018 ($)") ///
	ytitle("Change in Job Postings (%)" "from January to April 2020") ///
	${title_`version'} ///
	xlab(500 "$500" 1000 "$1,000" 1500 "$1,500" 2000 "$2,000") ///
	ylab(-50 "-50%" -45 "-45%" -40 "-40%" -35 "-35%" -30 "-30%" -25 "-25%", nogrid) ///
	ysc(ra(-50 -25)) xsc(ra(500 2050)) ///
	text( -50 500 "Slope = `slope'%/$1000 (s.e. = `se')", place(ne) just(left) color(gs8) size(medlarge))

oi_graph_export "${root}/results/Jobs/Job postings for Low-Education Workers vs Median Rent Binscatter", type(${fig_type})

* Black and white version for the QJE
binscatter change_april med_2br_2018 if jobzone == 1 [w = pop_2018] ///
	, ///
	aspectratio(0.5) mcolor(gs0) lcolor(gs8) ///
	xtitle("Median Two Bedroom Monthly Rent in 2014-2018 ($)") ///
	ytitle("Change in Job Postings (%)" "from January to April 2020") ///
	${title_`version'} ///
	xlab(500 "$500" 1000 "$1,000" 1500 "$1,500" 2000 "$2,000") ///
	ylab(-50 "-50%" -45 "-45%" -40 "-40%" -35 "-35%" -30 "-30%" -25 "-25%", nogrid) ///
	ysc(ra(-50 -25)) xsc(ra(500 2050)) ///
	text( -50 500 "Slope = `slope'%/$1000 (s.e. = `se')", place(ne) just(left) color(gs8) size(medlarge))

graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_4a.svg", replace
project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_4a.svg")

*-------------------------------------------------------------------------------
* Job postings for high-education workers vs median rent
*-------------------------------------------------------------------------------

reg change_april med_2br_2018  if jobzone == 2 [w = pop_2018], r
	local slope : di %4.2f _b[med_2br_2018] * 1000
	local se : di %4.2f _se[med_2br_2018] * 1000

binscatter change_april med_2br_2018 if jobzone == 2 [w = pop_2018] ///
	, ///
	aspectratio(0.5) ///
	xtitle("Median Two Bedroom Monthly Rent in 2014-2018 ($)") ///
	ytitle("Change in Job Postings (%)" "from January to April 2020") ///
	${title_`version'} ///
	xlab(500 "$500" 1000 "$1,000" 1500 "$1,500" 2000 "$2,000") ///
	ylab(-50 "-50%" -45 "-45%" -40 "-40%" -35 "-35%" -30 "-30%" -25 "-25%", nogrid) ///
	ysc(ra(-50 -25)) xsc(ra(500 2050)) ///
	text(-50 500 "Slope = `slope'%/$1000 (s.e. = `se')", place(ne) just(left) color(gs8) size(medlarge))

oi_graph_export "${root}/results/Jobs/Job postings for High-Education Workers vs Median Rent Binscatter", type(${fig_type})

* Black and white version for the QJE
binscatter change_april med_2br_2018 if jobzone == 2 [w = pop_2018] ///
	, ///
	aspectratio(0.5) mcolor(gs0) lcolor(gs8) ///
	xtitle("Median Two Bedroom Monthly Rent in 2014-2018 ($)") ///
	ytitle("Change in Job Postings (%)" "from January to April 2020") ///
	${title_`version'} ///
	xlab(500 "$500" 1000 "$1,000" 1500 "$1,500" 2000 "$2,000") ///
	ylab(-50 "-50%" -45 "-45%" -40 "-40%" -35 "-35%" -30 "-30%" -25 "-25%", nogrid) ///
	ysc(ra(-50 -25)) xsc(ra(500 2050)) ///
	text(-50 500 "Slope = `slope'%/$1000 (s.e. = `se')", place(ne) just(left) color(gs8) size(medlarge))

graph export "${root}/results/QJE_Figures_BlackAndWhite/Figure_4b.svg", replace
project, creates("${root}/results/QJE_Figures_BlackAndWhite/Figure_4b.svg")

yamlout using "${root}/results/paper numbers/`category'/Job Postings for High-Education Workers vs Rent (county).yaml", ///
	key("higheduc_rent_slope") ///
	comment("Slope (%/$1000)") ///
	value(`slope') fmt(%9.2f)
yamlout using "${root}/results/paper numbers/`category'/Job Postings for High-Education Workers vs Rent (county).yaml", ///
	key("higheduc_rent_se") ///
	comment("SE") ///
	value(`se') fmt(%9.2f)

project, creates("${root}/results/paper numbers/`category'/Job Postings for High-Education Workers vs Rent (county).yaml")

*-------------------------------------------------------------------------------
* Output numbers
*-------------------------------------------------------------------------------

gquantiles rent_ventile = med_2br_2018  [w = pop_2018] if jobzone == 1 & !mi(change_april), xtile nq(20)

sum change_april [w = pop_2018] if jobzone == 1 & rent_ventile == 1
assert r(mean) < 0
local change_rent_p5 = abs(r(mean))
sum change_april [w = pop_2018] if jobzone == 1 & rent_ventile == 20
assert r(mean) < 0
local change_rent_p100 = abs(r(mean))

local change_rent_high_low_pp: di %3.1f `change_rent_p100' - `change_rent_p5'
di `change_rent_high_low_pp'
local change_rent_high_low_pct: di %3.1f (`change_rent_p100' / `change_rent_p5' - 1) * 100
di `change_rent_high_low_pct'


yamlout using "${root}/results/paper numbers/`category'/Job Postings for Low-Education Workers vs Rent (county).yaml", ///
	key("loweduc_rent_slope") ///
	comment("Slope (%/$1000)") ///
	value(`slope') fmt(%9.2f)

yamlout using "${root}/results/paper numbers/`category'/Job Postings for Low-Education Workers vs Rent (county).yaml", ///
	key("loweduc_rent_se") ///
	comment("SE") ///
	value(`se') fmt(%9.2f)

yamlout using "${root}/results/paper numbers/`category'/Job Postings for Low-Education Workers vs Rent (county).yaml", ///
	key("change_rent_high_low_pp") ///
	comment("High-low rent pp diff in decline") ///
	value(`change_rent_high_low_pp') fmt(%9.1f)

yamlout using "${root}/results/paper numbers/`category'/Job Postings for Low-Education Workers vs Rent (county).yaml", ///
	key("change_rent_high_low_pct") ///
	comment("High-low rent pct diff in decline") ///
	value(`change_rent_high_low_pct') fmt(%9.1f)

project, creates("${root}/results/paper numbers/`category'/Job Postings for Low-Education Workers vs Rent (county).yaml")
